package cn.xfdzx.android.apps.shop.activity.procurement;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.ProcurementConfimListItemAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ProcurementTypeListBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitBeen;
import cn.xfdzx.android.apps.shop.bean.PurchaseSubmitEditBeen;
import cn.xfdzx.android.apps.shop.contract.ProcurementConfimListContract;
import cn.xfdzx.android.apps.shop.evenbus.PFCaiConfimNumMessage;
import cn.xfdzx.android.apps.shop.evenbus.PFCaiSelectNumMessage;
import cn.xfdzx.android.apps.shop.presenter.ProcurementConfimListPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;

//批发采购去订购   确认去支付
public class ProcurementConfimPayActivity extends BaseMvpActivity<ProcurementConfimListPresenter> implements ProcurementConfimListContract.View {

    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.tv_select_type_num)
    TextView tvSelectTypeNum;
    @BindView(R.id.tv_select_jian_num)
    TextView tvSelectJianNum;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.rv_procurement_list)
    RecyclerView rvProcurementList;
    private boolean isSave = false;//保存是否可以点击
    private Dialog dialog;

    private PurchaseSubmitBeen.DataBean dataBean;
    private ProcurementConfimListItemAdapter procurementConfimListItemAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_procurement_confim_pay;
    }

    @Override
    public void initView() {
        mPresenter = new ProcurementConfimListPresenter();
        mPresenter.attachView(this);
        EventBus.getDefault().register(this);
        dialog_delete();

        dataBean = (PurchaseSubmitBeen.DataBean) getIntent().getSerializableExtra("data");
        tvDate.setText("日期：" + dataBean.getDate());

        procurementConfimListItemAdapter = new ProcurementConfimListItemAdapter(this, dataBean.getGoods_list());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvProcurementList.setLayoutManager(linearLayoutManager);
        rvProcurementList.setItemAnimator(new DefaultItemAnimator());
        rvProcurementList.setNestedScrollingEnabled(false);
        rvProcurementList.setAdapter(procurementConfimListItemAdapter);

        tvSelectTypeNum.setText(dataBean.getGoods_list().size() + "");
        tvSelectJianNum.setText(dataBean.getGoods_num() + "");
        tvTotal.setText("共" + dataBean.getGoods_list().size() + "种，" + dataBean.getGoods_num() + "件。");
    }

    private void dialog_delete() {
        dialog = Utils.addDialog(this, R.layout.dialog, R.string.are_you_sure_to_delete, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mPresenter.purchaseDelete(dataBean.getPurchase_sn());
                    dialog.dismiss();
                }
            }
        });

    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(PFCaiConfimNumMessage messageEvent) {

        int total_jian_num = 0;

            for (PurchaseSubmitBeen.DataBean.GoodsListBean goodListbeen : dataBean.getGoods_list()) {
                total_jian_num+= goodListbeen.getGoods_num();

        }
        tvSelectJianNum.setText(total_jian_num + "");
    }

    @OnClick({R.id.iv_back, R.id.tv_update, R.id.tv_save, R.id.tv_delete, R.id.tv_go_pay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tv_go_pay:
                //去支付
                if (dataBean.getGoods_list().size() != 0) {

                }
                break;
            case R.id.tv_delete:
                dialog.show();
                break;
            case R.id.tv_update:    //
                if (!isSave) {
                    isSave = true;
                    tvUpdate.setBackgroundResource(R.drawable.db3b3b3_bg_r3);
                    tvSave.setBackgroundResource(R.drawable.d0ea9f9_bg_r3);
                    procurementConfimListItemAdapter.isUpdate(true);
                }
                break;
            case R.id.tv_save:    //
                if (isSave) {
                    isSave = false;
                    PurchaseSubmitEditBeen purchaseSubmitEditBeen = new PurchaseSubmitEditBeen();
                    purchaseSubmitEditBeen.setPurchase_sn(dataBean.getPurchase_sn());
                    for (PurchaseSubmitBeen.DataBean.GoodsListBean goodsListBean : dataBean.getGoods_list()) {
                        PurchaseSubmitEditBeen.UpdateDataBeen updateDataBeen = new PurchaseSubmitEditBeen.UpdateDataBeen();
                        updateDataBeen.setId(goodsListBean.getId());
                        updateDataBeen.setGoods_max_price(goodsListBean.getGoods_max_price());
                        updateDataBeen.setGoods_min_price(goodsListBean.getGoods_min_price());
                        updateDataBeen.setGoods_name(goodsListBean.getGoods_name());
                        updateDataBeen.setGoods_num(goodsListBean.getGoods_num());
                        updateDataBeen.setPurchase_type(goodsListBean.getPurchase_type());
                        purchaseSubmitEditBeen.getUpdate_data().add(updateDataBeen);
                    }
                    mPresenter.purchaseEdit(purchaseSubmitEditBeen);
                }
                break;
            case R.id.tv_go_dinggou:    //
                Intent intent = new Intent(this, ProcurementConfimPayActivity.class);
                startActivity(intent);
                break;

        }
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }

    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {
        isSave = false;
    }

    @Override
    public void onPurchaseEditSuccess(PurchaseSubmitBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            isSave = false;
            tvSave.setBackgroundResource(R.drawable.db3b3b3_bg_r3);
            tvUpdate.setBackgroundResource(R.drawable.d0ea9f9_bg_r3);
            dataBean.getGoods_list().clear();
            dataBean.setCategory_num(bean.getData().getCategory_num());
            dataBean.getGoods_list().addAll(bean.getData().getGoods_list());
            tvSelectTypeNum.setText(bean.getData().getGoods_list().size() + "");
            tvSelectJianNum.setText(bean.getData().getCategory_num() + "");
            tvTotal.setText("共" + dataBean.getGoods_list().size() + "种，" + dataBean.getGoods_num() + "件。");
            procurementConfimListItemAdapter.isUpdate(false);

        } else {
            Utils.toastMessage(this, bean.getMsg());
        }

    }

    @Override
    public void onPurchaseDeleteSuccess(BaseObjectBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            dataBean.getGoods_list().clear();
            tvSelectJianNum.setText("0");
            tvSelectTypeNum.setText(0 + "");
            tvTotal.setText("共" + 0 + "种，" + 0 + "件。");
            procurementConfimListItemAdapter.notifyDataSetChanged();
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }
}
